#include "graph.h"
#include "ui_graph.h"
#include<QDesktopWidget>
#include<QMessageBox>
#include<QtSql/QSqlQuery>
#include<QDate>
#include<QDebug>

Graph::Graph(int yy,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Graph)
{
     ui->setupUi(this);
     year=yy;
     QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );

     QRect q=QApplication::desktop()->availableGeometry(this);

     int margin=0,Awidth=0;
     /*if(q.width()<q.height())
        Awidth=q.width();
     else
         Awidth=q.height();*/
     Awidth=q.width();
     margin=Awidth/12.0;//For 240 width ,it is 20
     if(q.width()>500)
         margin=Awidth/18.0;
     int fontSize=6;
     ui->Vline->setGeometry(2.0*margin,margin,margin,(q.height()-(3*margin)));//Vertical
    qDebug()<<margin;
     ui->Hline->setGeometry((2.5*margin),(q.height()-(2.0*margin)),((q.width())-(3.0*margin)),margin/10.0);//Horizontal
     int avWidth=(Awidth-(ui->Vline->width()*4.0));//For 240, it is 160 available width
     int space=avWidth/40.0;//For 160 available width, it is 4
     int size=(avWidth/12.0)-space;
     int labelSize=4.1*size;
     if(q.width()>500)
         labelSize=2.1*size;
     ui->label_header->setFont(QFont( "lucida", 1.5*fontSize, QFont::Normal, TRUE));
     ui->label_header->setGeometry(1.5*margin,q.height()/3.0,q.width(),margin);
     ui->label_header->setVisible(false);
     int width=(ui->Vline->width()*3.0);


     ui->labelX->setAlignment(Qt::AlignRight);
     ui->labelX->setGeometry(0,(q.height()-(1.6*margin)),(2.5*margin),(1.5*size));
     ui->labelX->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
     ui->labelX->setText("Month");

     ui->progressBar1->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label1->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label1->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));


     width=width+ui->progressBar1->width()+space;
     ui->progressBar2->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label2->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label2->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar2->width()+space;
     ui->progressBar3->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label3->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label3->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar3->width()+space;
     ui->progressBar4->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label4->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label4->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar4->width()+space;
     ui->progressBar5->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label5->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label5->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar5->width()+space;
     ui->progressBar6->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label6->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label6->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar6->width()+space;
     ui->progressBar7->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label7->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label7->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar7->width()+space;
     ui->progressBar8->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label8->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label8->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar8->width()+space;
     ui->progressBar9->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label9->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label9->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar9->width()+space;
     ui->progressBar10->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label10->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label10->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar10->width()+space;
     ui->progressBar11->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label11->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label11->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

     width=width+ui->progressBar11->width()+space;
     ui->progressBar12->setGeometry(width,margin,size,(ui->Vline->height()));
     ui->label12->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->label12->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    /*width=width+ui->progressBar12->width();
    ui->labelXT->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
     ui->labelXT->setGeometry(width,(q.height()-(1.8*margin)),size,1.6*size);
     ui->labelXT->setText("X");*/
     ui->Vlabel1->setAlignment(Qt::AlignRight);
     ui->Vlabel2->setAlignment(Qt::AlignRight);
     ui->Vlabel3->setAlignment(Qt::AlignRight);
     ui->Vlabel4->setAlignment(Qt::AlignRight);
     ui->Vlabel5->setAlignment(Qt::AlignRight);

     ui->Vlabel1->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
     ui->Vlabel2->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
     ui->Vlabel3->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
     ui->Vlabel4->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
     ui->Vlabel5->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));

     ui->Vlabel1->setGeometry(0,margin-0.5*size,labelSize,1.3*size);
     ui->Vlabel2->setGeometry(0,((ui->Vline->height()*0.75)+ margin-0.5*size),labelSize,1.3*size);
     ui->Vlabel3->setGeometry(0,((ui->Vline->height()/2.0)+margin-0.5*size),labelSize,1.3*size);
     ui->Vlabel4->setGeometry(0,((ui->Vline->height()/4.0)+margin-0.5*size),labelSize,1.3*size);
     ui->Vlabel5->setGeometry(size,(ui->Vline->height()+margin-0.5*size),(labelSize-size),size);


    plotGraph();


}

Graph::~Graph()
{
    delete ui;
}
void Graph::back()
{
    (new ExpMonth(2,2))->show();
}
void Graph::plotGraph()
{
    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("finance_db");
    if(!db.open())
    {
        QMessageBox::information(0,"Error !","Error in opning database");

    }
    else
    {
        QDate dayOne,dayLast;
        dayOne.setDate(year,1,1);//First day of year
        dayLast.setDate(year,12,31);//Last day of year
        qDebug()<<dayLast;
        QSqlQuery qry;
        qry.prepare(QString("select * from expense_table where exp_date>=? and exp_date<=?"));
        qry.addBindValue(dayOne);
        qry.addBindValue(dayLast);
        if(qry.exec())
        {
            int count=0;
           double list[13];
           list[1]=list[2]=list[3]=list[4]=list[5]=list[6]=list[7]=list[8]=list[9]=list[10]=list[11]=list[12]=0;
            while(qry.next())
            {
                switch(qry.value(3).toDate().month())
                {
                case 1:list[1]=list[1]+qry.value(1).toDouble();
                       break;
                case 2:list[2]=list[2]+qry.value(1).toDouble();
                       break;
                case 3:list[3]=list[3]+qry.value(1).toDouble();
                       break;
                case 4:list[4]=list[4]+qry.value(1).toDouble();
                       break;
                case 5:list[5]=list[5]+qry.value(1).toDouble();
                       break;
                case 6:list[6]=list[6]+qry.value(1).toDouble();
                       break;
                case 7:list[7]=list[7]+qry.value(1).toDouble();
                       break;
                case 8:list[8]=list[8]+qry.value(1).toDouble();
                       break;
                case 9:list[9]=list[9]+qry.value(1).toDouble();
                       break;
                case 10:list[10]=list[10]+qry.value(1).toDouble();
                       break;
                case 11:list[11]=list[11]+qry.value(1).toDouble();
                       break;
                case 12:list[12]=list[12]+qry.value(1).toDouble();
                       break;
                default:qDebug()<<"Invalid Month";
                }
                count++;



            }
            if(count==0)
            {
              ui->label_header->setVisible(true);
              ui->label_header->setText(tr("No expense record found"));
              ui->Vline->setVisible(false);
              ui->Hline->setVisible(false);
              ui->progressBar1->setVisible(false);
              ui->progressBar2->setVisible(false);
              ui->progressBar3->setVisible(false);
              ui->progressBar4->setVisible(false);
              ui->progressBar5->setVisible(false);
              ui->progressBar6->setVisible(false);
              ui->progressBar7->setVisible(false);
              ui->progressBar8->setVisible(false);
              ui->progressBar9->setVisible(false);
              ui->progressBar10->setVisible(false);
              ui->progressBar11->setVisible(false);
              ui->progressBar12->setVisible(false);
              ui->labelX->setVisible(false);

            }
            else
            {
            double max=0;
            for(int i=1;i<=12;i++)
            {
                if(list[i]>max)
                    max=list[i];
            }
            qDebug()<<max;
            ui->progressBar1->setMaximum(max);
            ui->progressBar2->setMaximum(max);
            ui->progressBar3->setMaximum(max);
            ui->progressBar4->setMaximum(max);
            ui->progressBar5->setMaximum(max);
            ui->progressBar6->setMaximum(max);
            ui->progressBar7->setMaximum(max);
            ui->progressBar8->setMaximum(max);
            ui->progressBar9->setMaximum(max);
            ui->progressBar10->setMaximum(max);
            ui->progressBar11->setMaximum(max);
            ui->progressBar12->setMaximum(max);

            ui->progressBar1->setValue(list[1]);
            ui->progressBar2->setValue(list[2]);
            ui->progressBar3->setValue(list[3]);
            ui->progressBar4->setValue(list[4]);
            ui->progressBar5->setValue(list[5]);
            ui->progressBar6->setValue(list[6]);
            ui->progressBar7->setValue(list[7]);
            ui->progressBar8->setValue(list[8]);
            ui->progressBar9->setValue(list[9]);
            ui->progressBar10->setValue(list[10]);
            ui->progressBar11->setValue(list[11]);
            ui->progressBar12->setValue(list[12]);

            ui->label1->setText(tr("J"));
            ui->label2->setText(tr("F"));
            ui->label3->setText(tr("M"));
            ui->label4->setText(tr("A"));
            ui->label5->setText(tr("M"));
            ui->label6->setText(tr("J"));
            ui->label7->setText(tr("J"));
            ui->label8->setText(tr("A"));
            ui->label9->setText(tr("S"));
            ui->label10->setText(tr("O"));
            ui->label11->setText(tr("N"));
            ui->label12->setText(tr("D"));
            int maxi=max;
            qDebug()<<max;
            qDebug()<<maxi;
            ui->Vlabel1->setText(QString("%1").arg(maxi));
            ui->Vlabel2->setText(QString("%1").arg(maxi/4.0));
            ui->Vlabel3->setText(QString("%1").arg(maxi/2.0));
            ui->Vlabel4->setText(QString("%1").arg(maxi*0.75));
            ui->Vlabel5->setText("0");




        }
             db.close();
        }

    }
}
